﻿using System.Collections.Generic;
using Newtonsoft.Json;
using OrkutAPILibrary.Constants;
using OrkutAPILibrary.Entities;

namespace OrkutAPILibrary.Methods
{
    /// <summary>
    /// Orkut Comment Factory
    /// </summary>
    static class OrkutCommentFactory
    {
        const Formatting Formatting = new Formatting();

        /// <summary>
        /// Gets the photo comments.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="albumId">The album id.</param>
        /// <param name="photoId">The photo id.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutComment> GetPhotoComments(string userId, string albumId, string photoId, int startIndex = 0, int count = OrkutLibraryConstants.DefaultNoOfPictureComments)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "comments.get"},
                                    {"userId", userId},
                                    {"groupId", "@self"},
                                    {"type", "mediaitems"},
                                    {"albumId", albumId},
                                    {"mediaItemId", photoId},
                                    {"count", count.ToString()},
                                    {"startIndex", startIndex.ToString()}
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutComment>>(requestResponse);
        }

        /// <summary>
        /// Writes the photo comment.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="albumId">The album id.</param>
        /// <param name="photoId">The photo id.</param>
        /// <param name="commentBody">The comment body.</param>
        /// <returns></returns>
        internal static OrkutComment WritePhotoComment(string userId, string albumId, string photoId, string commentBody)
        {
            var commentToWrite = new OrkutComment
            {
                CommentBody = commentBody,
            };

            var set = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };

            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "comments.create"},
                                    {"userId", userId},
                                    {"groupId", "@self"},
                                    {"albumId", albumId},
                                    {"mediaItemId", photoId},
                                    {"type", "mediaitems"},
                                    {"comment", JsonConvert.SerializeObject(commentToWrite,Formatting, set)}
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);
            return string.IsNullOrEmpty(requestResponse)
                ? null :
                JsonConvert.DeserializeObject<OrkutComment>(requestResponse);
        }

        /// <summary>
        /// Deletes the photo comment.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="albumId">The album id.</param>
        /// <param name="photoId">The photo id.</param>
        /// <param name="commentId">The comment id.</param>
        /// <returns></returns>
        internal static bool DeletePhotoComment(string userId, string albumId, string photoId, string commentId)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "comments.delete"},
                                    {"userId", userId},
                                    {"groupId", "@self"},
                                    {"type", "mediaitems"},
                                    {"albumId", albumId},
                                    {"mediaItemId", photoId},
                                    {"type", "mediaitems"},
                                    {"commentId", commentId}
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);
            return string.IsNullOrEmpty(requestResponse)
                ? true :
                false;
        }
    }
}
